<?php
//ob_implicit_flush(1);
//header( 'Content-type:text/html; charset=utf-8');
include_once dirname(__FILE__).'/data/config.inc.php';
//echo str_repeat(' ', 4096)."<br />\n";
//echo str_repeat(" ", 80)."<br />\n";

//header( 'Content-type:text/html; charset=utf-8');
//echo "页面不存在 ...<br /><br />\n\n";
//echo "PHPSESSID			= ".$_COOKIE['PHPSESSID']."<br /><br />\n\n";
//echo "HTTP_HOST			= ".$_SERVER['HTTP_HOST']."<br /><br />\n\n";
//echo "REQUEST_URI		= ".$_SERVER['REQUEST_URI']."<br /><br />\n\n";
//echo "REQUEST_METHOD	= ".$_SERVER['REQUEST_METHOD']."<br /><br />\n\n";
//echo "REMOTE_ADDR		= ".$_SERVER['REMOTE_ADDR']."<br /><br />\n\n";
//echo "HTTP_USER_AGENT	= ".$_SERVER['HTTP_USER_AGENT']."<br /><br />\n\n";
//echo "SERVER_ADDR		= ".$_SERVER['SERVER_ADDR']."<br /><br />\n\n";
//echo "DOCUMENT_ROOT		= ".$_SERVER['DOCUMENT_ROOT']."<br /><br />\n\n";
//echo "SERVER_SOFTWARE	= ".$_SERVER['SERVER_SOFTWARE']."<br /><br />\n\n";

//echo "HTTP_REFERER	= ".getenv('HTTP_REFERER')."<br /><br />\n\n";
//echo "HTTP_REFERER	= ".$_SERVER['HTTP_REFERER']."<br /><br />\n\n";
//exit();

$__uri = array_shift(explode('?', $_SERVER['REQUEST_URI']));
$__uri = array_shift(explode('#', $__uri));
if(WEB_DIR == '/'){
	$__uri = ltrim($__uri, '/');
}else{
	$__uri = str_ireplace(WEB_DIR, '', $__uri);
}
$__uri = trim($__uri);
if(substr($__uri, -1, 1) == '/'){
	$__uri .= 'index.html';
}elseif(preg_match('/\/\w+$/i', $__uri)){
	$__uri .= '/index.html';
}

// 尝试转到相关PHP处理
$target = array(
	'readback'		=> array('/^readback\/(\w+)\.jpg$/i', ''),							// 邮件回执
	
	'goods_view'	=> array('/^pub\/goods\/201[3-9][01][0-9]\/((\d+)\.(html|mk))?$/i', ''),	// 产品详细
	'goods_search'	=> array('/^pub\/goods\/search.do$/i', ''),							// 产品搜索
	//'goods_index'	=> array('/^pub\/goods\/((\w+)\/)?index\.(html|mk)$/i', ''),		// 列表首页
	'goods_single'	=> array('/^pub\/goods\/v\/([_a-zA-Z-]{2,})\.(html|mk)$/i', ''),	// 产品详细
	'goods_list'	=> array('/^pub\/goods\/((\w+)\/)?(\d+|index)\.(html|mk)$/i', ''),	// 产品列表
	'goods_catetory'=> array('/^pub\/goods\/c\/(\d+)\/(\d+|index)\.(html|mk)$/i', ''),	// 产品分类
	
	'news_view'		=> array('/^pub\/news\/201[3-9][01][0-9]\/((\d+)\.(html|mk))?$/i', ''),	// 新闻详细
	//'news_index'	=> array('/^pub\/news\/((\w+)\/)?index\.(html|mk)$/i', ''),			// 列表首页
	'news_single'	=> array('/^pub\/news\/v\/([_a-zA-Z-]{2,})\.(html|mk)$/i', ''),		// 新闻单页
	'news_list'		=> array('/^pub\/news\/((\w+)\/)?(\d+|index)\.(html|mk)$/i', ''),	// 新闻列表
	
	'brand_view'	=> array('/^pub\/brand\/v\/((\d+)\.(html|mk))?$/i', ''),			// 品牌详细
	//'brand_index'	=> array('/^pub\/brand\/index\.(html|mk)$/i', ''),					// 列表首页
	'brand_single'	=> array('/^pub\/brand\/v\/([_a-zA-Z-]{2,})\.(html|mk)$/i', ''),	// 品牌单页
	'brand_list'	=> array('/^pub\/brand\/(\d+|index)\.(html|mk)$/i', ''),			// 品牌列表

	'categorys'		=> array('/^pub\/goods\/cats\.(html|mk)$/i', ''),					// 所有分类
	'ads_view'		=> array('/^pub\/ads\/((\d+)\.(html|mk))?$/i', ''),					// 广告详细

	'sitemap'		=> array('/^pub\/sitemap\.(html|mk)$/i', array('mdl' => 'sitemap',)),					// 广告详细
	
	'index'			=> array('/^index\.(html|mk)$/i', array('mdl' => 'index')),		// 首页
	//'mk' 			=> array('/\.(mk)$/i', ''),											// 静态文件
);

$_entry = 'index';
$_REQUEST['mdl']	= '404';
foreach($target as $key => $val){
	$i = 0;
	if(preg_match($val[0], $__uri, $match)){
		
		if(!empty($val[1]) && is_array($val[1])){			
			$_REQUEST = array_merge($_REQUEST, $val[1]);
		}else{
			switch($key){	// 需要特殊处理
				case 'readback':
					$_entry = 'auto';
					$_REQUEST['mdl']	= 'readback';
					$_GET['sign']		= $match[1];
					break;
					
				case 'goods_view':
					$_REQUEST['mdl']	= 'goods';
					$_REQUEST['do']		= 'view';
					$_GET['id']			= $match[2];
					break;
				case 'goods_single':
					$_REQUEST['mdl']	= 'goods';
					$_REQUEST['do']		= 'view';
					$_GET['filename']	= $match[1];
					break;
				case 'goods_search':
					$_REQUEST['mdl']	= 'goods';
					$_GET['s']			= 1;
					$_GET['page']		= empty($match[4]) ? 1 : intval($match[4]);
					break;
				// case 'goods_index':
				// 	$_REQUEST['mdl']	= 'goods';
				// 	$_GET['mark']		= trim($match[2]);	// 标识、目录
				// 	$_GET['page']		= 1;
				// 	break;
				case 'goods_list':
					$_REQUEST['mdl']	= 'goods';
					$_GET['mark']		= trim($match[2]);	// 标识、目录
					$_GET['page']		= empty($match[3]) ? 1 : intval($match[3]);
					break;
				case 'goods_catetory':
					$_REQUEST['mdl']	= 'goods';
					$_GET['catid']		= trim($match[1]);	// 标识、目录
					$_GET['page']		= empty($match[2]) ? 1 : intval($match[2]);
					break;
					
				case 'news_view':
					$_REQUEST['mdl']	= 'news';
					$_REQUEST['do']		= 'view';
					$_GET['id']			= $match[2];
					break;
				case 'news_single':
					$_REQUEST['mdl']	= 'news';
					$_REQUEST['do']		= 'view';
					$_GET['filename']	= $match[1];
					break;
				// case 'news_index':
				// 	$_REQUEST['mdl']	= 'news';
				// 	$_GET['mark']		= trim($match[2]);	// 标识、目录
				// 	$_GET['page']		= 1;
				// 	break;
				case 'news_list':
					$_REQUEST['mdl']	= 'news';
					$_GET['mark']		= trim($match[2]);	// 标识、目录
					$_GET['page']		= empty($match[3]) ? 1 : intval($match[3]);
					break;
					
				case 'brand_view':
					$_REQUEST['mdl']	= 'brand';
					$_REQUEST['do']		= 'view';
					$_GET['id']			= $match[2];
					break;
				case 'brand_single':
					$_REQUEST['mdl']	= 'brand';
					$_REQUEST['do']		= 'view';
					$_GET['mark']	= $match[1];
					break;
				// case 'brand_index':
				// 	$_REQUEST['mdl']	= 'brand';
				// 	$_GET['page']		= 1;
				// 	break;
				case 'brand_list':
					$_REQUEST['mdl']	= 'brand';
					$_GET['page']		= empty($match[1]) ? 1 : intval($match[1]);
					break;
					
				case 'categorys':
					$_REQUEST['mdl']	= 'goods';
					$_REQUEST['do']		= 'cats';
					break;
					
				case 'ads_view':
					$_REQUEST['mdl']	= 'ads';
					$_REQUEST['do']		= 'view';
					$_GET['id']			= $match[2];
					break;
			}
			//echo $key.' = ';print_r($match);	// 调试
		}
		break;		// 一旦匹配到就跳出循环
	}
}
if(empty($_GET['page']))$_GET['page'] = 1;
if($_REQUEST['mdl'] != '404' && $_GET['page'] <= 20){	// 用于生成静态页面
	if(preg_match('/(.mk|.html|mk=yes)/i', $__uri)){
		$_GET['mk'] = 'yes';
		// $__uri = str_replace('.mk', '.html', $__uri);
	}
}
// 还原搜索参数
$_arr = explode('?', $_SERVER['REQUEST_URI']);
if(!empty($_arr[1])){
	parse_str($_arr[1], $get);
	// $get = array_map('htmlspecialchars', $get);	// 加了360sage，这里就先不用了
	$_GET = array_merge($_GET, $get);
}

// 生成静态文件
if(isset($_GET['mk']) && $_GET['mk'] == 'yes'){
	$__filename = WEB_PTH.str_replace('mk', 'html', $__uri);
	if(isset($_GET['remake']) && $_GET['remake'] == 'yes'){
		if(file_exists($__filename)){
			unlink($__filename);
			exit;
		}
	}
}

include_once dirname(__FILE__)."/{$_entry}.php";
?>